<template>
  <div class="customer-opennewsignature-container">
    <top-tip :tipcontent="tipcontent" />
    <vab-query-form>
      <vab-query-form-left-panel :span="14">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.cityArea"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="选择行业"
                clearable
              />
              <!-- collapse-tags -->
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="请输入客户名称"
                @input="trimkey"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="请输入挑入人"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.showType"
      type="border-card"
      @tab-click="handleTabs"
    >
      <el-tab-pane label="我的公海新签资源" :name="0">
        <el-table
          v-loading="listLoading"
          :data="mylist"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="入库ID"
            prop="CustomerPickID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="区域"
            prop="City"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="行业"
            prop="Industry"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="地址"
            prop="Address"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="电话"
            prop="Telphone"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="挑入时间"
            prop="PickTime"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="客户标记" width="200">
            <template #default="{ row }">
              <el-tag v-for="(itm, idx) in row.CustomerMarks" :key="idx">
                {{ itm }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="商机跟进" width="100">
            <template #default="{ row }">
              <el-button text @click="getCustomerTrack(row)">
                商机跟进
              </el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button text @click="handleEdit(row)">备注客户信息</el-button>
            </template>
          </el-table-column> -->
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="下属公海新签资源" :name="1">
        <el-table
          v-loading="listLoading"
          :data="sublist"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="入库ID"
            prop="CustomerPickID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="挑入人"
            prop="PickStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="挑入时间"
            prop="PickTime"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="客户标记" width="200">
            <template #default="{ row }">
              <el-tag v-for="(itm, idx) in row.CustomerMarks" :key="idx">
                {{ itm }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="跟进记录" width="100">
            <template #default="{ row }">
              <el-button text @click="getCustomerTrack(row)">
                跟进记录
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button text @click="handleEdit(row)">备注客户信息</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { CitySelect, IndustrySelect } from '@/api/common'
  import { getList, doDelete } from '@/api/customerOpennewsignature'
  import Edit from './components/CustomerOpennewsignatureEdit'
  import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'OpenNewSignature',
    components: { Edit, TopTip },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tipcontent: {
          title: '公海新签资源',
          memo:
            '所有公海新签资源：所有的，在人员库中，从未合作过的、公海挑入/新增录入/申请挑入的客户资源；<br>' +
            '下属公海新签资源：在管辖范围的人员库中，从未合作过的、公海挑入/新增录入/申请挑入的客户资源；<br>' +
            '我的公海新签资源：在自己库中，从未合作过的、公海挑入/新增录入/申请挑入的客户资源；<br>',
        },
        editRef: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        mylist: [],
        mytotal: 0,
        sublist: [],
        cityOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industryOptions: [],
        industrySelect: { checkStrictly: true, multiple: true },
        subtotal: 0,
        alllist: [],
        alltotal: 0,
        total: 0,
        selectRows: '',
        queryForm: {
          pickPeople: '',
          showType: 0,
          pickType: 0,
          cityArea: {},
          industry: {},
          searchKey: '',
          pickStaff: '',
          pageNo: 1,
          pageSize: 10,
        },
      })
      const trimkey = (val) => {
        state.queryForm.searchKey = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
      }
      const handleTabs = (tab) => {
        console.log('handleTabs', tab.index, tab.props.name)
        state.queryForm.showType = tab.props.name
        state.queryForm.pageNo = 1
        fetchData()
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const getCustomerTrack = (row) => {
        router.push({
          path: '/customer/track/trackingBusiness',
          query: { CustomerPickID: row.CustomerPickID },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        if (state.queryForm.showType === 0) {
          state.mylist = list
          state.total = total
        }
        if (state.queryForm.showType === 1) {
          state.sublist = list
          state.total = total
        }
        if (state.queryForm.showType === -1) {
          state.alllist = list
          state.total = total
        }

        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })
      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }

      return {
        ...toRefs(state),
        handleTabs,
        setSelectRows,
        handleEdit,
        getCustomerTrack,
        handleDelete,
        getCitySelect,
        getIndustrySelect,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
        trimkey,
      }
    },
  })
</script>
